/*
 * Team Name: Team Sasquatch
 * Cody Sharff
 * Anthony Rios
 * Prince Kishore
 * Travis Drake
 */

package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import java.util.Observable;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SpringLayout;

import model.ErrorType;
import model.Item;
import model.User;
import model.UserType;

/**
 * This class contains a JPanel that will display information
 * about a particular item.
 * 
 * @author Cody Sharff
 *
 */
public class ItemViewGUI extends Observable{
	
	
	/**
	 * JPanel main window title
	 */
    public static final String WINDOW_TITLE = "Item Information";
    
    /**
     * String constant used to signal the observer to return to the previous screen.
     */
    public static final String BACK = "Back";
	
	/**
	 * The JPanel that will be used to display on screen.
	 */
	private final JPanel my_item_panel;
	
	/**
	 * The item whose information will be displayed by the panel.
	 */
	private final Item my_item;
	
	/**
	 * The user who is currently viewing the item information.
	 */
	private final User my_user;
	
	/**
	 * The information about a particular item.
	 */
	private final String my_item_info[] = new String[7];

	/**
	 * The items information fields.
	 */
	private final String my_fields[] = new String[6];
	
	/**
	 * Back button used to return to previous screen.
	 */
	private final JButton my_back_button = new JButton("Back");
	
	/**
	 * Bid button that allows a bidder to place a bid on an item.
	 */
	private final JButton my_bid_button = new JButton("Bid");
	
	/**
	 * Text field that allows a bidder to enter their bid.
	 */
    private final JTextField my_text_field = new JTextField(20);
	
    /**
     * Constructs an ItemViewGUI object.
     * 
     * @param the_item the item to display
     * @param the_user the user who is viewing the item.
     */
	public ItemViewGUI(Item the_item, User the_user) {
		my_item = the_item;
		my_user = the_user;
		my_item_panel = new JPanel(new BorderLayout());
		my_item_panel.setName("Item Information");
		createItemInfoArray();
		createItemFieldArray(the_item);
		addListeners();
		layoutComponents();
	}

	/**
	 * Assigns the fields to an array to be displayed on the panel.
	 */
	private void createItemInfoArray() {
		my_item_info [0] = "Item Name: ";
		my_item_info [1] = "Item Quantity: ";
		my_item_info [2] = "Minimum Starting Bid: ";
		my_item_info [3] = "Donor: ";
		my_item_info [4] = "Size: ";
		my_item_info [5] = "Storage: ";
		my_item_info [6] = "Place Bid: ";
	}
	
	/**
	 * Gets information from the item to display next the to item
	 * info labels.
	 */
	private void createItemFieldArray(Item the_item) {
		NumberFormat format = NumberFormat.getCurrencyInstance();
		
		my_fields[0] = the_item.getItemName();
		my_fields[1] = String.valueOf(the_item.getQuantity());
	 
		my_fields[2] = format.format((double) (the_item.getMinStartingBid() / 100.0));
		my_fields[3] = the_item.getDonor();
		my_fields[4] = String.valueOf(the_item.getSize());
		my_fields[5] = the_item.getStorage();
	}

	/**
	 * Adds actionListeners to the buttons.
	 */
	private void addListeners() {
	    my_back_button.addActionListener(new AbstractAction() {
	        public void actionPerformed(final ActionEvent the_event) {
	          setChanged();
	          notifyObservers(BACK);
	        }
	      });
	    my_bid_button.addActionListener(new AbstractAction() {
	        public void actionPerformed(final ActionEvent the_event) {
	        	if(!my_text_field.getText().isEmpty()) {
	        		ErrorType bid_success = my_item.addBid(my_user.getName(), parseInput(my_text_field.getText()));
	        		if (bid_success == ErrorType.MORE_THAN_ONE_BID_PER_ITEM) {
	        			JOptionPane.showMessageDialog(null, "Cannot bid on the same item twice", "Error", JOptionPane.ERROR_MESSAGE);
	        		} else if (bid_success == ErrorType.BID_BELOW_MINIMUM) {
	        			JOptionPane.showMessageDialog(null, "Bid lower than minimum bid", "Error", JOptionPane.ERROR_MESSAGE);
	        		} else {
	        			JOptionPane.showMessageDialog(null, "Your bid has been accepted");
	        		}
	        	} else {
	        		JOptionPane.showMessageDialog(null, "Please enter bid!", "Error", JOptionPane.ERROR_MESSAGE);
	        	}
	        }
	      });
	}
	
	/**
	 * Turns the string input used for placing a bid and returns it in
	 * integer form.
	 * 
	 * @before a string formatted in the following format: $$$...$$$$.$$
	 * @after a positive integer that represents currency in pennies
	 */
	public static int parseInput(String text) {
		return (Integer.parseInt(text.substring(0, text.length()-3)) * 100)
				+ Integer.parseInt(text.substring(text.length()-2, text.length()));
	}

	/**
	 * Configures the different components that make up the GUI and
	 * places them in specified locations.
	 */
	public void layoutComponents() {
		int rows = my_item_info.length;
		final JPanel spring_layout = new JPanel();
		spring_layout.setLayout(new SpringLayout());
        for (int i = 0; i < my_fields.length; i++) {
        	spring_layout.add(new JLabel(my_item_info[i]));
        	spring_layout.add(new JLabel(my_fields[i]));
        }
        if(my_user.getType() == UserType.BIDDER) {
        	spring_layout.add(new JLabel(my_item_info[6]));
        	spring_layout.add(my_text_field);
        } else {
        	rows--;
        }
        SpringUtilities.makeCompactGrid(spring_layout,
        		rows, 2,        //rows, cols
                265, 150,        			   //initX, initY
                8, 8);       				   //xPad, yPad
        my_item_panel.add(spring_layout, BorderLayout.NORTH);
        JPanel south_panel = new JPanel(new FlowLayout());
        south_panel.add(my_back_button);
        if(my_user.getType() == UserType.BIDDER) {
        	south_panel.add(my_bid_button);
        }
        my_item_panel.add(south_panel, BorderLayout.SOUTH);
	}
	
	/**
	 * Returns the JPanel.
	 */
    public JPanel getPanel() {
    	return my_item_panel;
    }
}
